//
//  PywRealNameAuthManager.m
//  PywSdk
//
//  Created by Ampaw on 2017/5/20.
//  Copyright © 2017年 Jade. All rights reserved.
//

#import "PywRealNameAuthManager.h"
#import "PywPlatformMain.h"
#import "PywUnbundAlertView.h"
#import "PywAlertView.h"
#import "PywFileManager.h"
#import "AppUtil.h"
#import "PywMasonry.h"
#import "PywSdk+login.h"
#import "PywLogoutManger.h"
#import "PywCPNotifications.h"
#import "PywNavgationController.h"
#import "PywRealNameAuthViewController.h"
#import "PywBindGetSMSCodeController.h"
#import "PywUploadAccountLoginManager.h"
#import "PywBuriedManager.h"

@interface PywRealNameAuthManager ()<PywUnbundAlertViewDelegate,PywAlertViewDelegate>
@property(nonatomic, strong) UIViewController *supViewController;
@end

@implementation PywRealNameAuthManager

+ (PywRealNameAuthManager *)manager
{
    static id manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[[self class] alloc] init];
    });
    return manager;
}

- (void)showRealNameAuthViewController
{
    dispatch_async(dispatch_get_main_queue( ),^{
        
        // Modal出“实名认证”控制器，加载Web端实名制认证
        if ([PywPlatformMain sharedPlatformMain].is_verify_show) {
            PywRealNameAuthViewController *realNameAuthVC = [[PywRealNameAuthViewController alloc] init];
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:realNameAuthVC animated:NO completion:NULL];
        }
    });
}

- (void)dismissViewController:(UIViewController *)supViewController isVerify:(BOOL)isVerify
{
    dispatch_async(dispatch_get_main_queue( ),^{
        
        self.supViewController = supViewController;
        // 已认证
        if (isVerify) {
            
            // 游戏账号登录 或 快速注册并登录，并且未绑定通行证
            if ([PywPlatformMain sharedPlatformMain].is_game_fast_login
                && [PywPlatformMain sharedPlatformMain].passport.length == 0) {
                
                // 弹出“绑定手机”弹框
                PywUnbundAlertView *alertView = [[PywUnbundAlertView alloc] initWithTitle:@"未绑定手机"
                                                                                  message:@"忘记密码可用手机找回\n可登录所有朋友玩游戏"
                                                                             messageImage:@"img_emoji03"
                                                                                 delegate:self
                                                                          sureButtonTitle:@"立即绑定"];
                [alertView show];
                // 保存当前弹出的时间
                [[PywFileManager manager] setFileValue:[NSNumber numberWithLong:[AppUtil getNowTimeStamp]] forFileKey:@"lastOpenDialogTime"];
            }else{
                
                [supViewController dismissViewControllerAnimated:NO completion:NULL];
            }
        }else{
            // 弹出退出框，点击“确定”注销SDK，点击“取消”再次弹出“实名认证”框
            PywAlertView *alertView = [[PywAlertView alloc] initWithTitle:@"温馨提示"
                                                                  message:@"是否退出游戏？"
                                                               subMessage:nil
                                                             messageImage:@"img_emoji02"
                                                         messageAlignment:MessageAlignmentHorizontal
                                                                 delegate:self
                                                        cancelButtonTitle:@"取消"
                                                          sureButtonTitle:@"退出"
                                       ];
            [[UIApplication sharedApplication].keyWindow addSubview:alertView];
            [alertView mas_makeConstraints:^(PywMASConstraintMaker *make) {
                make.top.bottom.left.right.offset(0);
            }];
        }
    });
}

#pragma mark - PywUnbundAlertViewDelegate
/**
 *  select Button
 *
 *  @param alertView   this AlertView
 *  @param buttonIndex select  ButtonIndex
 */
- (void)bundAlertView:(PywUnbundAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    dispatch_async(dispatch_get_main_queue( ),^{
        if (buttonIndex == 1) { // 点击“确定”，跳转绑定
            [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"3" actionSn:@"9" typeSn:@"3"];
            [self.supViewController dismissViewControllerAnimated:NO completion:^{
                
                PywBindGetSMSCodeController *getCodeView = [[PywBindGetSMSCodeController alloc] init];
                PywNavgationController *nav = [[PywNavgationController alloc] initWithRootViewController:getCodeView];
                [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:NULL];
            }];
        }else{
            [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"3" actionSn:@"8" typeSn:@"3"];
            [self.supViewController dismissViewControllerAnimated:NO completion:NULL];
        }
    });
}

#pragma mark - PywAlertViewDelegate
- (void)alertView:(PywAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    dispatch_async(dispatch_get_main_queue( ),^{
        
        if (buttonIndex == 1) { // 退出
            // 注销账号，保存当前登录账号结束时间
            [[PywUploadAccountLoginManager manager] saveAccountLoginEndTime];
            
            [[PywLogoutManger manger] clearLogin];
            [self performSelector:@selector(delayMethod) withObject:nil afterDelay:0.5];
        }else{ // 点击“取消”，再次弹出“实名认证”框
            PywRealNameAuthViewController *realNameAuthVC = [[PywRealNameAuthViewController alloc] init];
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:realNameAuthVC animated:NO completion:NULL];
        }
    });
}
- (void)delayMethod
{
    // 发送注销完成的通知
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:@"注销成功" forKey:@"info"];
    [dict setValue:[NSString stringWithFormat:@"%d",PYW_LOGOUT_STATE_NORMAL] forKey:@"result"];
    [[NSNotificationCenter defaultCenter] postNotificationName:kPywCPLogoutNotification object:nil userInfo:dict];
    
    [self exitApplication];
}

/**
 退出App程序
 */
- (void)exitApplication {
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    
    [UIView animateWithDuration:1.0f animations:^{
        window.alpha = 0;
        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
}

@end
